package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffPsnjobListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecPsnjobManager;
import cn.ffcs.uec.staff.model.UecPsnjob;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;

/**
 * @Title:任职信息 CRUD插件
 * @Description:
 * @Company: FFCS
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecOutStaffPsnjobListboxExt extends Div implements IdSpace {
	
	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecOutStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecOutStaffPsnjobListboxBean bean = new UecOutStaffPsnjobListboxBean();
	/**
	 * 人员语言能力实体
	 */
	private UecPsnjob uecPsnjob;

	private UecPsnjobManager uecPsnjobManager = (UecPsnjobManager) ApplicationContextUtil.getBean("uecPsnjobManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffPsnjobListboxExt() {
		Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_PSNJOB_EXT_LISTBOX_ZUL, this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_PSNJOB_QUERY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNJOB, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_PSNJOB_RESPONSE);

	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
		//判断当前登录人员是否是盟市主管：true  不是，false    是
		 boolean noMainOutStaff = PermissionUtil.isNoMainOutStaff();
		 if(noMainOutStaff) {
			 bean.getA1().setVisible(false);
		 }
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception  
	 */
	public void onSelectUecOutStaffPsnjobList() throws Exception {
		if (this.bean.getUecOutStaffPsnjobListbox().getSelectedCount() > 0) {
			uecPsnjob = (UecPsnjob) bean.getUecOutStaffPsnjobListbox().getSelectedItem().getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffPsnjobQueryResponse(ForwardEvent event) {
		uecOutStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (null != uecOutStaffInfo) {
	        //设置操作按钮，非盟市主管不可操作按钮
	        if(PermissionUtil.isNoMainOutStaff()) {
	            this.setButtonValid(false, false, false, false);
	        }else {
	            this.setButtonValid(true, false, true, false);
	        }
			uecPsnjob = new UecPsnjob();
			uecPsnjob.setStaffId(uecOutStaffInfo.getOutStaffId());
			uecPsnjob.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			this.onQueryUecOutStaffPsnjob();
		}
	}

	public void onCleanUecOutStaffPsnjobResponse(ForwardEvent event) {
		clearVariable();
		uecOutStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecOutStaffPsnjobListbox());
		this.bean.getUecOutStaffPsnjobListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd, boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecOutStaffPsnjob() {
		this.bean.getUecOutStaffPsnjobListboxPaging().setActivePage(0);
		this.onQueryUecOutStaffPsnjobList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecOutStaffPsnjob() {
		ListboxUtils.clearListbox(this.bean.getUecOutStaffPsnjobListbox());
		this.bean.getUecOutStaffPsnjobListboxPaging().setTotalSize(1);
	}

	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecOutStaffPsnjob() throws Exception, InterruptedException {
		if (null != uecPsnjob.getId() && uecOutStaffInfo != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecPsnjob", uecPsnjob);
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_PSNJOB_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		}else {
			ZkUtil.showError("请选中要查看的记录！", "提示信息");
			return;
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * 			@throws
	 */
	public void onAddUecOutStaffPsnjob() throws Exception {
		if (null != uecOutStaffInfo) {
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkHrorg())) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("opType", "add");
				map.put("uecOutStaffInfo", uecOutStaffInfo);
				Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_PSNJOB_EDIT_ZUL,
						this, map);
				window.doModal();
				window.addEventListener("onOK", new EventListener() {
					@Override
					public void onEvent(Event event) throws Exception {
						if (event.getData() != null) {
							// 新增或修改完成后，列表从数据库更新
							uecPsnjob = (UecPsnjob) event.getData();
							onQueryUecOutStaffPsnjobList();
						}
					}
				});
			}
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecOutStaffPsnjob() throws Exception, InterruptedException {
		if (null != uecPsnjob.getId()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecPsnjob", uecPsnjob);
			map.put("uecOutStaffInfo", uecOutStaffInfo);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_PSNJOB_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecPsnjob = (UecPsnjob) event.getData();
						onQueryUecOutStaffPsnjobList();
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecOutStaffPsnjob() {
		if (this.uecPsnjob.getId() != null && uecOutStaffInfo != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecPsnjob.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecOutStaffPsnjobListbox(), uecPsnjob, "del");
						uecPsnjob = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecOutStaffPsnjobListboxPaging() {
		this.onQueryUecOutStaffPsnjobList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecPsnjob = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecOutStaffPsnjobList() {
		if (null != uecPsnjob) {
			PageInfo pageInfo = uecPsnjobManager.queryPageInfoByUecPsnjob(uecPsnjob,
					this.bean.getUecOutStaffPsnjobListboxPaging().getActivePage() + 1,
					this.bean.getUecOutStaffPsnjobListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(), true);
			this.bean.getUecOutStaffPsnjobListbox().setModel(list);
			this.bean.getUecOutStaffPsnjobListboxPaging().setTotalSize(pageInfo.getTotalCount());
		}
	}

}
